/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.incad.psh.pshassign.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.FocusPanel;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import cz.incad.psh.pshassign.client.res.Resources;
import cz.incad.psh.pshassign.client.rpc.PSHService;
import cz.incad.psh.pshassign.client.rpc.PSHServiceAsync;
import java.util.Iterator;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author alberto
 */
public class Institutions extends Composite {
    
    private static InstitutionsUiBinder uiBinder = GWT.create(InstitutionsUiBinder.class);
    PSHServiceAsync pshService = GWT.create(PSHService.class);
    @UiField
    HTMLPanel content;
    Grid table;
    JSONObject json;
    
    interface InstitutionsUiBinder extends UiBinder<Widget, Institutions> {
    }
    
    public Institutions() {
        initWidget(uiBinder.createAndBindUi(this));
        open();
    }
    
    void open() {
        try {
            pshService.getInstitutions(new AsyncCallback() {
                @Override
                public void onFailure(Throwable caught) {
                    Window.alert("error getting configuration: "
                            + caught.getLocalizedMessage());
                }
                
                @Override
                public void onSuccess(Object result) {
                    json = JSONParser.parseStrict((String) result).isObject();
                    Set keys = json.keySet();
                    table = new Grid(keys.size(), 4);
                    
                    int row = 0;
                    Iterator it = keys.iterator();
                    while (it.hasNext()) {
                        String key = (String) it.next();
                        JSONObject inst = json.get(key).isObject();
                        table.setText(row, 1, inst.get("label").isString().stringValue());
                        JSONArray hesla = inst.get("hesla").isArray();
                        HorizontalPanel fp = new HorizontalPanel();
                        for (int j = 0; j < hesla.size(); j++) {
                            //fp.add(new Institution(hesla.get(j).isString().stringValue()));
                            FlowPanel fc = new FlowPanel();
//                            fc.addClickHandler(new ClickHandler() {
//                                @Override
//                                public void onClick(ClickEvent event) {
//                                    throw new UnsupportedOperationException("Not supported yet.");
//                                }
//                            });
                            String label = hesla.get(j).isString().stringValue();
                            fc.setStyleName(Resources.INSTANCE.css().institution());
                            fc.add(new Label(label));
                            fp.add(fc);
                            fc.addDomHandler(new InstitutionClickHandler(label), ClickEvent.getType());
                        }
                        table.setWidget(row, 2, fp);
                        row++;
                    }
                    content.add(table);
                }
            });
        } catch (Exception ex) {
            Logger.getLogger(Institutions.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    class InstitutionClickHandler implements ClickHandler{
        String label;
        InstitutionClickHandler(String label){
            this.label = label;
        }
        
        @Override
        public void onClick(ClickEvent event) {
            Window.alert(this.label);
        }
        
    }
}
